package b3.model
{
	import b3.net.*;
	import b3.util.*;
	import b3.widget.WidgetManager;
	import b3.widget.basic.*;
	
	public class GuildManager extends Model
	{
		private static var instance:GuildManager ;
		public var myGuildId:int; 
		
		public function GuildManager()
		{
			this.myGuildId = -1;
		}
		
		public function loginGuild():void
		{
			var serverRequest:ServerRequest = new ServerRequest(ServerEvent.LOGIN_GUILD);
			serverRequest.setHandleFunc(handleLoginGuild);
			serverRequest.post(true);
		}
		
		private function handleLoginGuild(data:Object):void
		{
			this.myGuildId = data['guildId'];
		}
		
		static public function getInstance():GuildManager
		{
			if (!instance)
			{
				instance = new GuildManager();
			}
			return instance;
		}
		
		public function createGuild(guildName:String):void 
		{
			var serverRequest:ServerRequest = new ServerRequest(ServerEvent.CREATE_GUILD, {guildName: guildName});
			serverRequest.setHandleFunc(handleCreateGuild);
			serverRequest.post(true);
		}
		
		private function handleCreateGuild(data:Object):void
		{//获得公会信息
			this.myGuildId = data['guildId'];
			var popMessage:PopMessage;
			popMessage = new PopMessage("恭喜您创建公会成功", 0xFF0000);
			WidgetManager.getInstance().popOut([popMessage]);
		//显示成员列表
			
		}
		
		public function quitGuild():void
		{
			var userId:int = PlayerManager.getInstance().myUserId;
			var serverRequest:ServerRequest = new ServerRequest(ServerEvent.KICK_MEMBERS, {memberId:userId,guildId:myGuildId});
			serverRequest.setHandleFunc(handleQuitGuild);
			serverRequest.post(true);
		}
		
		private function handleQuitGuild(data:Object):void
		{//离开公会
			this.myGuildId = -1;
			var popMessage:PopMessage;
			popMessage = new PopMessage("成功退出公会", 0xFF0000);
			WidgetManager.getInstance().popOut([popMessage]);
		//刷新公会列表 
		}
		
		public function sendJoinRequest(guildId:int):void
		{
			var serverRequest:ServerRequest = new ServerRequest(ServerEvent.REQUEST_JOIN_GUILD, {guildId:guildId});
			serverRequest.setHandleFunc(handleJoinRequest);
			serverRequest.post(true);
		}
		
		private function handleJoinRequest(data:Object):void
		{
			var text:String = new String;
			if (data['err'] == 100 )
			{
				text = "您已经是一个公会的成员";
			}
			else if (data['err'] == 101 )
			{
				text = "您已经向该公会发出过请求";
			}else
			{
				text = "请求发送成功";
			}
			var popMessage:PopMessage;
			popMessage = new PopMessage(text, 0xFF0000);
			WidgetManager.getInstance().popOut([popMessage]);
		}
		
		//批准请求
		public function sendAdminRequest(userId:int):void
		{
			var serverRequest:ServerRequest = new ServerRequest(ServerEvent.ADMIN_REQUEST, {guildId:myGuildId,requestId: userId});
			serverRequest.setHandleFunc(handleSendAdminRequest);
			serverRequest.post(true); 
		}
		
		private function handleSendAdminRequest(data:Object):void
		{
			var text:String = new String;
			if (data['err'] == 1000 )
			{
				text = "无法添加该成员";
			}
			else
			{
				text = "成功接纳该会员";
			}
			var popMessage:PopMessage;
			popMessage = new PopMessage(text, 0xFF0000);
			WidgetManager.getInstance().popOut([popMessage]);
		}

	}
}